﻿using CashAgent.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CashAgent
{
    public partial class FrmStoreSelect : Form
    {
        public FrmStoreSelect()
        {
            InitializeComponent();
        }

        private void FrmStoreSelect_Load(object sender, EventArgs e)
        {
            //绑定店铺列表
            string storeJson = RequestUtil.Reqest("/open/store/lists", "");
            dataGridView1.DataSource = JsonConvert.DeserializeObject<List<EntityStore>>(storeJson);

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择店铺");
                return;
            }

            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            configuration.AppSettings.Settings.Add("storeId", dataGridView1.SelectedRows[0].Cells["gcId"].Value.ToString());
            configuration.AppSettings.Settings.Add("storeName", dataGridView1.SelectedRows[0].Cells["gcBranchName"].Value.ToString());
            // Save the configuration file.    
            configuration.AppSettings.SectionInformation.ForceSave = true;
            configuration.Save(ConfigurationSaveMode.Modified);
            // Force a reload of the changed section.    
            ConfigurationManager.RefreshSection("appSettings");   


            //Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
            //configuration.AppSettings.Settings.Add("storeId", dataGridView1.SelectedRows[0].Cells["gcId"].Value.ToString());
            //configuration.AppSettings.Settings.Add("storeName", dataGridView1.SelectedRows[0].Cells["gcBranchName"].Value.ToString());
            //configuration.AppSettings.SectionInformation.ForceSave = true;
            //configuration.Save(ConfigurationSaveMode.Modified);
            //ConfigurationManager.RefreshSection("appSettings");
            DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
